package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.service.*;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.framework.cache.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目Controller
 * @author wy
 * @email wy
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/biz/bizProject")
public class BizProjectController extends BaseController {

    @Autowired
    private BizProjectService bizProjectService;
    @Autowired
    private BizOrderService bizOrderService;

    /**
     * 查询项目列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizProject bizProject, HttpServletRequest request, HttpServletResponse response) {
        bizProject.setPage(new PageDomain(request, response));
        PageInfo<BizProject> page = bizProjectService.findPage(bizProject);
        for (int i = 0; i < page.getList().size(); i++) {
            List<BizOrder> bizOrders = bizOrderService.getBizOrderByProjectId(page.getList().get(i).getId());
            page.getList().get(i).setBizOrders(bizOrders);
        }
        return R.data(page);
    }
    /**
     * 新增项目
     */
    @Log(title = "新增项目", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizProject bizProject) {
        return R.status(bizProjectService.createProject(bizProject));
    }

    /**
     * 修改项目
     */
    @Log(title = "修改项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizProject bizProject) {
        return R.status(bizProjectService.updateProject(bizProject));
    }

    /**
     * 删除项目
     */
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable String id) {
        BizProject bizProject = bizProjectService.get(id);
        return R.status(bizProjectService.remove(bizProject));
    }
}
